class AddPositionStandard  < Mutations::Command
  include Wisper::Publisher

  required do
    string  :position_id
    string  :requre_certificate_organ
    array   :certificate_ids
  end

  def execute
    unless @requre_certificate_organ = RequireCertificateOrgan.find_by_name(requre_certificate_organ)
      add_error(:requre_certificate_organ, :requre_certificate_organ, '持证要求组织不存在')
      return
    end
    position_standard = PositionCertificateStandard.where(position_id: position_id, requre_certificate_organ: requre_certificate_organ).first || PositionCertificateStandard.new(position_id: position_id, requre_certificate_organ: @requre_certificate_organ)


    if position_standard.save
      position_standard.update_certificates certificate_ids 
      return position_standard
    else
      position_standard.errors.each do |key, value|
        add_error(key, key, value)
      end
    end
  end
end